<?php
/*
 * 后台模块树类别
 * author wangjian
 * time 2013-12-15
 *
 */
if (! defined('BASEPATH')) {
	exit('Access Denied');
}

class Tree_type extends MY_Controller{
	private $username ;//当前登录的用户
	private $ip ; //登录的ip地址
	private $dir = './data/tree'; 
	private $table_ ; //表的前缀
	
	function Tree_type(){
		parent::__construct();
		$this->load->library('admin_common');//加载admin的公用方法
		$this->load->model('admin/tree/M_tree');
		date_default_timezone_set('Asia/Shanghai');
		$this->load->model('admin/M_log');//加载日志模型
		$this->username = $this->get_login_name(); //得到登录的用户名
		$this->ip = $this->admin_common->get_client_ip();
		$this->table_ = $this->admin_common->table_pre('real_data');
	}
	
	function typelist(){
		$data_ = $_REQUEST ;
		$action = 'typelist' ;
		$action_array = array(
			'typelist','ajax_data'
		) ;
		$action = (isset($data_['action']) && in_array($data_['action'], $action_array))?$data_['action']:'typelist';
		if($action == 'typelist'){
			$this->load->view('admin/tree/views_type_list');
		}elseif($action == 'ajax_data'){
			$this->ajax_type_list() ;
		}
	}
	//类别添加处理
	function tree_type_add(){
		$data_ = $_POST ;
		$typename = $this->admin_common->do_addslashes(strip_tags(htmlspecialchars($data_['typename'])));  
		$data = array(
			'typename'=>$typename,
			'addtime'=>date("Y-m-d H:i:s",time()),
			'addperson'=>$this->username,
		) ;
		$code = 0 ;
		$message = '' ;
		$insert_array = $this->M_tree->insert_by_table_name($data,$this->table_."common_tree_type");
		if($insert_array['affect_num']>0){
			//添加成功
			$code = 1 ;
			$message = '添加成功';
			$this->M_log->insert($insert_array['sql'],'log_add_treetype',$this->username,$this->ip,1,"添加类别是:{$typename}成功"); //插入日志记录
			
			echo $this->admin_common->result_to_towf_new('',$code,$message,null);
			die();
		}else{
			//添加失败
			$code = 1 ;
			$message = '添加失败';
			$this->M_log->insert($insert_array['sql'],'log_add_treetype',$this->username,$this->ip,0,"添加类别是:{$typename}失败"); //插入日志记录
			echo $this->admin_common->result_to_towf_new('',$code,$message,null);
			die();
		}
	}
	function update_type_name(){
		$data_ = $_REQUEST ;
		$action = 'update' ;
		$action_array = array(
			'update','do_update'
		) ;
		$action = (isset($data_['action']) && in_array($data_['action'], $action_array))?$data_['action']:'update';
		if($action == 'update'){
			$id = intval(isset($data_['id'])?$data_['id']:0);
			if($id<=0){
				showmessage("参数传递错误",'admin/admin_tree/tree_type/typelist',3,0);
				die();
			}
		$sql = "SELECT * FROM `{$this->table_}common_tree_type` where id = '{$id}'" ;
		$res = $this->M_tree->querylist($sql,false);	
		if(isset($res[0]) && !empty($res[0])){
			$data['row'] = $res[0];
			$this->load->view('admin/tree/views_type_edit',$data);
		}else{
			showmessage("参数传递错误",'admin/admin_tree/tree_type/typelist',3,0);
			die();
		}
		}elseif($action == 'do_update'){
			$this->do_edit_typename();
		}
		//$this->do_edit_typename();
	}
	
	//类别列表
	private function ajax_type_list(){
		
		$post_data = $this->input->post(NULL,true) ;
		$data = array();
		if(!isset($post_data['page'])){
			$page  =1 ;
		}else{
			$page = intval($post_data['page']);
		}	
		$page = ($page == 0)?1:$page ;
		$sql_count = "SELECT count(*) as tt FROM `{$this->table_}common_tree_type` ";
		$total = $this->M_tree->query_count($sql_count);
	
		$per_page = 10;//每一页显示的数量
		$page_string = $this->page_string($total, $per_page, $page);
		$limit = ($page-1)*$per_page;
		$sql = "SELECT * FROM `{$this->table_}common_tree_type` order  by id desc LIMIT ".$limit.",".$per_page;
		$res = $this->M_tree->querylist($sql,false);
		$status_array = array(
			0=>'<font color="red">无效</font>',
			1=>'有效'
		);
		if($res){
			foreach($res as $k=>$v){
				$res[$k]['status'] = isset($status_array[$v['status']])?$status_array[$v['status']]:'' ;
				$res[$k]['ismake'] = (file_exists($this->dir."/tree_".$v['id']."_format.txt"))?'已经生成':'<font color="red">未生成</font>';
				$res[$k]['filetime'] = (file_exists($this->dir."/tree_".$v['id']."_format.txt"))?date ("Y-m-d H:i:s", filemtime($this->dir."/tree_".$v['id']."_format.txt")):'<font color="red">暂无</font>';
				$res[$k]['filepath'] = (file_exists($this->dir."/tree_".$v['id']."_format.txt"))?$this->dir."/tree_".$v['id']."_format.txt":'<font color="red">未生成</font>';
			}
		}
		$data['result'] = $res ;
		$data['page_string'] = $page_string ;	
		echo $this->admin_common->result_to_towf_new($data,'','',null);
	}	
	private function do_edit_typename(){
		$data_ = $_REQUEST;
		$id = intval($data_['id']);
		if($id<=0){
			showmessage("参数传递错误",'admin/admin_tree/tree_type/typelist',3,0);
			die();
		}
		$typename = $this->admin_common->do_addslashes(strip_tags(htmlspecialchars(isset($data_['typename'])?$data_['typename']:'')));  
		if(empty($typename))
		{
			exit('error');
		}
		$status = intval($data_['status']);
		$edit = "UPDATE `{$this->table_}common_tree_type` SET `typename` = '{$typename}',`status` = '{$status}' WHERE `id` = '{$id}'";
		$num = $this->M_tree->update_data($edit);
		if($num>=1){
			
			$this->M_log->insert($edit,'log_edit_treetype',$this->username,$this->ip,1,"修改id为{$id}模块类别记录成功"); //插入日志记录
			showmessage("修改成功",'admin/admin_tree/tree_type/typelist',3,1);
			
			die();
		}else{
			
			$this->M_log->insert($edit,'log_edit_treetype',$this->username,$this->ip,0,"修改id为{$id}模块类别记录失败"); //插入日志记录
			showmessage("服务器繁忙请稍候",'admin/admin_tree/tree_type/typelist',3,0);
			die();		
		}
	}

	
	/*@function page_string
	 *@分页返回数据
	 *@total 总的数量
	 *@page_num 每一页显示的数量
	 *@page 当前是第几页数据
	 */
	private function page_string($total,$page_num,$page){
		$page_string = '' ;
		$this->load->library('pagination');//加载分页类
		$this->load->library('MY_Pagination');//加载分页类
		$config['total_rows'] = $total;
		$config['use_page_numbers'] =true; // 当前页结束样式
		$config['per_page'] = $page_num; // 每页显示数量，为了能有更好的显示效果，我将该数值设置得较小
		$config['full_tag_open'] = '<div class="pg">'; // 分页开始样式
		$config['full_tag_close'] = '</div>'; // 分页结束样式
		$config['first_link'] = '首页'; // 第一页显示
		$config['last_link'] = '末页'; // 最后一页显示
		$config['next_link'] = '下一页 >'; // 下一页显示
		$config['prev_link'] = '< 上一页'; // 上一页显示
		$config['cur_tag_open'] = ' <a class="current">'; // 当前页开始样式
		$config['cur_tag_close'] = '</a>'; // 当前页结束样式
		$config['uri_segment'] = 6;
		$config['anchor_class']='class="ajax_page" ';
		$this->pagination->cur_page = $page ;
		$this->pagination->initialize($config); // 配置分页
		$page_string =  $this->pagination->create_links(true,'ajax_data');
		return $page_string ;
	}	
}


